package com.umeox.watch.moto.launcher.ui;


import static com.umeox.watch.moto.launcher.ui.ThemeSettingActivity.ACTION_THEME_LOADING_COMPLETED;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.umeox.moto.watch.themes.app.ThemeManager;
import com.umeox.moto.watch.themes.skin.loader.ResourceManager;
import com.umeox.moto.watch.themes.util.ThemeUtils;
import com.umeox.watch.moto.launcher.R;


/**
 * Created by Rambo 2019-11-23
 * Describe:
 */
public class ThemeLoadingActivity extends AppCompatActivity {


    public static final String EXTRA_THEME = "theme_index";
    private ImageView mDoneIv;
    private ImageView mLoadingIv;
    private RotateAnimation mRotateAnimation;

    private int mThemeIndex;
    private TextView mMessageTv;
    private ThemeManager mThemeManager;
    private ImageView mTitleBarIv;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        mThemeManager = ThemeManager.getInstance();
        setTheme(mThemeManager.getCurrentStyle(this));
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_theme_loading);
        initListener();
        initView();
        initData();
    }

    private void initView() {
        mMessageTv = findViewById(R.id.tv_msg);
        mTitleBarIv = findViewById(R.id.iv_title_bar);
        mLoadingIv = findViewById(R.id.iv_loading);
        mDoneIv = findViewById(R.id.iv_done);
        mTitleBarIv.setImageResource(R.drawable.theme_top_icon);
        mDoneIv.setImageResource(R.drawable.gallery_confirm_delete_done);
        mLoadingIv.setImageResource(R.drawable.gallery_loading);
        startAnimation();
    }


    private void initListener() {
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ACTION_THEME_LOADING_COMPLETED);
        registerReceiver(mBroadcastReceiver, intentFilter);
    }


    private void initData() {
        mThemeIndex = getIntent().getIntExtra(EXTRA_THEME, 0);
        final String[] themeNames = getResources().getStringArray(R.array.theme_name);
        final String themeName = themeNames[mThemeIndex];
        final int color = ThemeUtils.textColorTertiary(this, R.color.dark_secondary_text_color);
        int pixelSize = 18;
        String uploading = String.format(getString(R.string.theme_loading), themeName);
        int index = uploading.indexOf(String.valueOf(themeName));
        ColorStateList redColors = ColorStateList.valueOf(color);
        TextAppearanceSpan textAppearanceSpan = new TextAppearanceSpan("sans-serif-condensed", 0, pixelSize, redColors, null);
        SpannableStringBuilder spanBuilder = new SpannableStringBuilder(uploading);
        spanBuilder.setSpan(textAppearanceSpan, index, index+themeName.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        mMessageTv.setText(spanBuilder);


        mDoneIv.postDelayed(() -> {
            ResourceManager.getInstance().load(mThemeIndex);
            mThemeManager.setCurrentTheme(mThemeIndex);
        }, 200);
    }

    private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            mMessageTv.setText(R.string.theme_load_completed);
            mRotateAnimation.cancel();
            mLoadingIv.clearAnimation();
            mLoadingIv.setVisibility(View.GONE);
            mDoneIv.setVisibility(View.VISIBLE);
            mDoneIv.postDelayed(ThemeLoadingActivity.this::finish, 1200);
        }
    };

    public static void onLaunch(Context context, int position) {
        Intent intent = new Intent(context, ThemeLoadingActivity.class);
        intent.putExtra(EXTRA_THEME, position);
        context.startActivity(intent);
    }

    private void startAnimation() {
        mRotateAnimation = new RotateAnimation(0, // 起始角度
                359,// 终止的角度
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f);
        mRotateAnimation.setDuration(200);
        mRotateAnimation.setInterpolator(new LinearInterpolator());
        mRotateAnimation.setRepeatCount(Animation.INFINITE);
        mLoadingIv.startAnimation(mRotateAnimation);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }
}
